//
//  Favorites_VC.m
//  WorldOfFacts_V2
//
//  Created by Demjanko Denis on 20.11.12.
//  Copyright (c) 2012 it-dimension. All rights reserved.
//

#import "Favorites_VC.h"
#import "DataBase.h"
#import "FavoriteCell.h"
#import "DetailFact_VC.h"

@implementation Favorites_VC

#pragma mark - init

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)setNavTitle:(NSString *)title
{
    UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        
        titleView.textColor = [UIColor colorWithRed:187.0f/255.0f green:187.0f/255.0f blue:187.0f/255.0f alpha:1.0f]; // Change to desired color
        
        self.navigationItem.titleView = titleView;
    }
    
    titleView.text = title;
    [titleView sizeToFit];
}

#pragma mark - View Life

- (void)viewDidLoad{
    [super viewDidLoad];
    [self registerNotifications];
    [self setNavTitle:@"Мои закладки"];
    
    UIButton *edit_button = [[UIButton alloc] init];
    [edit_button setBackgroundImage:[UIImage imageNamed:@"edit_button"] forState:UIControlStateNormal];
    [edit_button setBackgroundImage:[UIImage imageNamed:@"edit_button_done"] forState:UIControlStateSelected];
    [edit_button setFrame:CGRectMake(0, 0, 43, 34)];
    [edit_button addTarget:self action:@selector(onEdit:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:edit_button];
    
    factsArray = [[NSMutableArray alloc] initWithArray:[[DataBase sharedManeger] getFavorites]];
    
    if(!factsArray.count)
        noFavoritesView.alpha = 1.0f;
    else
        noFavoritesView.alpha = 0.0f;
}

#pragma mark - IBActions

-(void)onEdit:(UIButton*)sender{
    sender.selected = !sender.selected;
    [table setEditing:sender.selected animated:YES];
}

#pragma mark - notifications

-(void)registerNotifications{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onChangeFavorites:) name:@"CHANGE_FAVORITES" object:nil];
}

-(void)removeNotificatios{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"CHANGE_FAVORITES" object:nil];
}

-(void)onChangeFavorites:(NSNotification*)notification{
    int fact_number = [[notification.object valueForKey:@"fact_number"] intValue];
    int is_favorite = [[notification.object valueForKey:@"is_favorite"] intValue];
    int fact_id = [[notification.object valueForKey:@"fact_id"] intValue];

    if(is_favorite==0)
        [factsArray removeObjectAtIndex:fact_number];
    else
        [factsArray insertObject:[NSMutableDictionary dictionaryWithDictionary:[[DataBase sharedManeger]getMiniFact:fact_id]] atIndex:0];
    
    if(!factsArray.count)
        noFavoritesView.alpha = 1.0f;
    else
        noFavoritesView.alpha = 0.0f;
    
    [table reloadData];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return factsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    
    FavoriteCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[FavoriteCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];                        
    }
    
    [cell setFact:[factsArray objectAtIndex:indexPath.row]];
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DetailFact_VC *vc = [[DetailFact_VC alloc] initWithNibName:@"DetailFact_VC" bundle:nil];
    vc.fact_id = [[[factsArray objectAtIndex:indexPath.row] valueForKey:@"fact_id"] intValue];
    vc.fact_number = indexPath.row;
    vc.fact_type = 0;
    vc.category_id = 0;
    vc.fact_mode = 2;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
   
    [[DataBase sharedManeger] updateFavorites:[[[factsArray objectAtIndex:indexPath.row] valueForKey:@"fact_id"]intValue] is_Favorite:0];
    [factsArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
    if(!factsArray.count)
        noFavoritesView.alpha = 1.0f;
}

#pragma mark - Memory Management

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

-(void)dealloc{
    factsArray = nil;  
    [self removeNotificatios];
}

@end
